package copyright;

import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.*;

/**
 * .
 *
 * @author sunhao(sunhao.java @ gmail.com)
 * @date 2018-1-19-0019 16:15
 */
public class GenerateCopyright {
    private static final String[] MODULES = new String[]{
            "lodsve-all",
            "lodsve-amqp",
            "lodsve-cache",
            "lodsve-core",
            "lodsve-dfs",
            "lodsve-mongodb",
            "lodsve-mybatis",
            "lodsve-redis",
            "lodsve-search",
            "lodsve-security",
            "lodsve-test",
            "lodsve-validate",
            "lodsve-web",
            "lodsve-wechat",
            "lodsve-workflow"
    };
    private static final String[] COPYRIGHT = new String[]{
            "/*",
            " * Copyright (C) 2018  Sun.Hao",
            " *",
            " * This program is free software: you can redistribute it and/or modify",
            " * it under the terms of the GNU General Public License as published by",
            " * the Free Software Foundation, either version 3 of the License, or",
            " * (at your option) any later version.",
            " *",
            " * This program is distributed in the hope that it will be useful,",
            " * but WITHOUT ANY WARRANTY; without even the implied warranty of",
            " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the",
            " * GNU General Public License for more details.",
            " *",
            " * You should have received a copy of the GNU General Public License",
            " * along with this program.  If not, see <http://www.gnu.org/licenses/>.",
            " */"

    };
    private static final List<String> EXCLUDE_FOLDER_NAMES = new ArrayList<>(Arrays.asList("ansi", "condition"));
    private static final String BASE_PATH = "/Users/sunhao/Documents/workspaces/lodsve/lodsve-framework/";
    private static final String SRC_PATH = "/src/main/java";

    private static final Map<String, List<File>> MODULE_FILES = new HashMap<>();

    public static void main(String[] args) throws IOException {
        for (String module : MODULES) {
            String path = BASE_PATH + module + SRC_PATH;
            File moduleFile = new File(path);
            List<File> files = listFiles(moduleFile);

            MODULE_FILES.put(module, files);
        }

        int size = 0;
        for (String module : MODULE_FILES.keySet()) {
            List<File> files = MODULE_FILES.get(module);

            size += files.size();
            System.out.println("====" + module + " start ====" + module + "共有" + files.size() + "个文件！");
            for (File file : files) {
                //addCopyright(file);
                System.out.println(file.getName() + "===已处理好！");
            }
            System.out.println("====" + module + " end ====");
            System.out.println("\n\n");
        }

        System.out.println("****总共有" + size + "个文件！****");
    }

    private static List<File> listFiles(File parent) {
        List<File> files = new ArrayList<>();
        File[] children = parent.listFiles();
        if (children == null || children.length == 0) {
            return files;
        }

        for (File child : children) {
            String name = child.getName();
            if (child.isDirectory()) {
                if (EXCLUDE_FOLDER_NAMES.contains(name)) {
                    continue;
                }
                files.addAll(listFiles(child));
            } else if (child.isFile()) {
                if (!name.endsWith(".java")) {
                    continue;
                }
                files.add(child);
            }
        }

        return files;
    }

    private static void addCopyright(File file) throws IOException {
        if (file == null || !file.exists() || file.isDirectory()) {
            return;
        }

        byte[] bytes = IOUtils.toByteArray(new FileInputStream(file));
        RandomAccessFile f = new RandomAccessFile(file, "rw");
        f.seek(0);
        for (String line : COPYRIGHT) {
            f.write((line + "\n").getBytes());
        }
        f.write("\n".getBytes());
        f.write(bytes);
        f.close();
    }
}
